package com.uton.rfidcontroller.view.activity;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.uton.rfidcontroller.R;
import com.uton.rfidcontroller.common.GlobalConstants;
import com.uton.rfidcontroller.common.rfid.UfhData;
import com.uton.rfidcontroller.common.utils.Utils;
import com.uton.rfidcontroller.databinding.ActivitySettingBinding;
import com.uton.rfidcontroller.view.base.BaseActivity;
import com.uton.rfidcontroller.viewmodel.activity_model.SettingViewModel;

import butterknife.BindView;
import io.reactivex.ObservableTransformer;

/**
 * Created by 王家宝 on 2017/9/11.
 * Email:639586819@qq.com
 * Tel:18241150668
 */

public class SettingActivity extends BaseActivity<ActivitySettingBinding, SettingViewModel> {

    @BindView(R.id.tv_state)
    TextView tvState;

    private Handler mHandler;
    private boolean isDestroy;

    @Override
    protected int setLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void inject() {
        getBinding().setModel(getViewModel());
    }

    @Override
    protected String setTitle() {
        return getString(R.string.ui_title_scan_mode);
    }

    @Override
    protected void doSomething() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                super.handleMessage(msg);
                switch (msg.what) {
                    case GlobalConstants.MSG_SHOW_PROPERTIES:
                        Utils.showShortToast(getString(R.string.toast_open_scan_mode));
                        if (!isDestroy) {
                            tvState.setText(R.string.ui_scan_open);
                            finish();
                        }
                        break;
                    case GlobalConstants.MSG_CLOSE_SCAN:
                        Utils.showShortToast(getString(R.string.toast_close_scan_mode));
                        if (!isDestroy) {
                            tvState.setText(R.string.ui_scan_close);
                        }
                        break;
                    default:
                        break;
                }
            }
        };

        //设置扫描仪状态
        if (UfhData.isDeviceOpen()) {
            tvState.setText(R.string.ui_scan_open);
        } else {
            tvState.setText(R.string.ui_scan_close);
        }

    }

    @Override
    protected SettingViewModel setViewModel() {
        return new SettingViewModel() {
            @Override
            public void sendOpenMessage() {
                mHandler.sendEmptyMessage(GlobalConstants.MSG_SHOW_PROPERTIES);
            }

            @Override
            public void sendCloseMessage() {
                mHandler.sendEmptyMessage(GlobalConstants.MSG_CLOSE_SCAN);
            }

            @Override
            public ObservableTransformer setObservable() {
                return compose(bindToLifecycle());
            }
        };
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroy = true;
    }
}
